package com.googlecode.jsimplemarket.wicket;

import com.googlecode.jsimplemarket.wicket.pages.LoginPage;
import java.net.MalformedURLException;
import org.apache.wicket.Page;
import org.apache.wicket.Session;
import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.request.Request;
import org.apache.wicket.request.Response;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.apache.wicket.util.file.Folder;
import org.springframework.security.authentication.AuthenticationManager;
import org.wicketstuff.security.hive.HiveMind;
import org.wicketstuff.security.hive.config.PolicyFileHiveFactory;
import org.wicketstuff.security.swarm.SwarmWebApplication;

/**
 * @author alex
 *
 */
public class MarketApp extends SwarmWebApplication {
    @SpringBean(name = "authenticationManager")
    private AuthenticationManager authenticationManager;
    @SpringBean
    private String uploadPath;
    private Folder uploadFolder;

    public AuthenticationManager getAuthenticationManager() {
        return authenticationManager;
    }

    public void setAuthenticationManager(AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }

    public String getUploadPath() {
        return uploadPath;
    }

    public void setUploadPath(String uploadPath) {
        this.uploadPath = uploadPath;
    }

    public Folder getUploadFolder() {
        return uploadFolder;
    }

    public void setUploadFolder(Folder uploadFolder) {
        this.uploadFolder = uploadFolder;
    }
    
    public static MarketApp get() {
        return (MarketApp) WebApplication.get();
    }

    /* (non-Javadoc)
     * @see org.apache.wicket.protocol.http.WebApplication#init()
     */
    @Override
    protected void init() {
        super.init();
        getComponentInstantiationListeners().add(new SpringComponentInjector(this));
        uploadFolder = new Folder(uploadPath, "market");
        uploadFolder.mkdirs();
    }

    @Override
    public Class<? extends Page> getHomePage() {
        return LoginPage.class;
    }

    @Override
    public final Session newSession(Request request, Response response) {
        return new SecuredWebSession(this, request);
    }

    @Override
    protected void setUpHive() {
        final String policyFileUrl = "/WEB-INF/wicket-springsec.hive";
        PolicyFileHiveFactory factory = new PolicyFileHiveFactory(this
                .getActionFactory());
        try {
            factory.addPolicyFile(getServletContext().getResource(policyFileUrl));
        } catch (MalformedURLException e) {
            throw new WicketRuntimeException("Error while loading resource " + policyFileUrl, e);
        }

        HiveMind.registerHive(getHiveKey(), factory);
    }

    @Override
    protected Object getHiveKey() {
        return getServletContext().getContextPath();
    }

    @Override
    public Class<? extends Page> getLoginPage() {
        return LoginPage.class;
    }    
}
